<?php
require_once('./includes/headers.inc.php');
require_once('./top.php');

if(!in_array($_SESSION['user']['id'], $admin_users)) {
    $_SESSION['msg'] = $mgrMgr->get('access-denied-error');
    header('Location: /');
    exit();
}

$error = FALSE;
$location_id = $_GET['id'];

if($_POST) {
    $ret = $locationMgr->update($location_id, $_POST);
    if($ret) {
        $_SESSION['msg'] = $msgMgr->get('edit-location-success');
        header('Location: /viewlocation.php?id=' . $location_id);
        exit();
    } else {
        $error = $msgMgr->get('edit-location-error');
    }
}

$location = $locationMgr->get($location_id);

$replaces = array(
    'name'          => (isset($_POST['name']) ? $_POST['name'] : $location['name']),
    'description'   => htmlentities(isset($_POST['description']) ? $_POST['description'] : $location['description']),
    'error'         => $msgMgr->formatMsg($error),
);
$lang = array(
    'header_location_edit'  => $langMgr->get('header_location_edit'),
    'text_name'             => $langMgr->get('text_name'),
    'text_description'      => $langMgr->get('text_description'),
    'btn_save'              => $langMgr->get('btn_save'),
);
$templateMgr->loadContent(basename(__FILE__, '.php'), array_merge($replaces, $lang));
?>
